// miniprogram/pages/recruitment_show_page/recruitment_show_page.js

const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    animationTime: 1,
    recruitmentInfo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRecruitmentPageInfo(options.recruitmentId);
    // this.getRecruitmentPageInfo(1);
  },

  /**
   * 加载职位详情
   */
  getRecruitmentPageInfo: function (recruitmentId) {
    let that = this;
    let url = config.SERVER_URL + 'index/recruitment/' + recruitmentId;
    wx.showLoading({
      title: '加载ing'
    })
    request.requestGetApi(url, {}, {}, 'application/json').then(res=>{
      if (res.status === config.STATUS_OK) {
        this.setData({
          recruitmentInfo: res.data
        });
      };
    }).catch(err=>{
      app.failFun(err);
    }).finally(res=>{
      app.completeFun(res)
    })
  },

  /**
   * 申请职位
   */
  applyToRecruitment: function(e){
    let that = this;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL + 'teams/userApplyToTeam';
    let teamId = this.data.recruitmentInfo.teamId;
    let recruitmentId = this.data.recruitmentInfo.id;
    let params = {
      teamId: teamId,
      recruitmentId: recruitmentId
    };
    wx.showModal({
      title: '提示',
      content: '申请加入该职位？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '发送申请ing',
          })
          request.requestPostApi(url, token, params, 'application/json').then(res=>{
            if(res.status === config.STATUS_OK){
              wx.showToast({
                title: '申请成功~',
                icon: 'success',
                duration: 3000
              });
            }else if(res.status === 400){
              wx.showToast({
                title: res.message,
                icon: 'none',
                duration: 3000
              });
            }
          }).catch(err=>{
            app.failFun(err);
          }).finally(res=>{
            app.completeFun(res);
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },

})